<template>
	<view class="ljapp articlePublish">
		<lj-top-tip :type="status_txt" :text="reason" v-if="status_txt"></lj-top-tip>
		
		<view class="content">
			<view class="form-group">
				<view class="form-item">
					<u--input
						v-model="obj.title"
						border="none"
						fontSize="24rpx"
						placeholderStyle="color: #666666"
						placeholder="输入文章标题"
					></u--input>
				</view>
				<view class="form-item" style="margin-top: 38rpx;">
					<u--input
						v-model="obj.keyword"
						border="none"
						fontSize="24rpx"
						placeholderStyle="color: #666666"
						placeholder="输入文章副标题"
					></u--input>
				</view>
				<view class="form-item">
					<u-upload
						:fileList="img"
						name="img"
						maxCount="1"
						uploadText="上传封面图"
						width="160rpx"
						height="130rpx"
						uploadIcon="plus"
						@afterRead="uploadSuccess"
						@delete="img=[],obj.img=''"
					></u-upload>
				</view>
			</view>
			<view class="form-group">
				<view class="form-label">文章内容</view>
				<view class="form-item">
					<view style="padding: 20rpx 26rpx;">
						<u--textarea v-model="obj.content" placeholder="请输入文章内容" height="574rpx" count :maxlength="2000"></u--textarea>
					</view>
				</view>
			</view>
			
			<view class="btn-row">
				<view class="btn" @click="submit">{{obj.id?'提交保存':'提交发布'}}</view>
			</view>
		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				status: -10, // -10未提交 0.待审核 1.审核通过
				status_txt: '',
				reason: '',
				
				//提交需求
				obj: {
					id: 0,
					title: '',
					keyword: '',
					img: '',
					content: '',
				},
				img: [],
			}
		},
		onLoad(options) {
			if(options.id){
				this.obj.id = options.id
				uni.setNavigationBarTitle({
					title: "编辑文章"
				})
			}
			this.init();
		},
		methods: {
			async uploadSuccess(e) {
				console.log(e, )
				// this.$set(this.arr, index, value);
				this[e.name] = [{
					...(e.file),
					status: 'uploading',
					message: '上传中'
				}]

				const result = await this.uploadFilePromise(this[e.name][0].url)
				if(result.code == 1){
					this[e.name] = [{
						status: 'success',
						message: '',
						url: result.data.fullurl
					}]
					this.obj[e.name] = result.data.url
				}else{
					this[e.name] = [{
						status: 'failed',
						message: '上传失败'
					}]
				}
			},
			uploadFilePromise(url, uploadurl) {
				var that = this
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: that.$Config.http_static+'api/common/uploader',
						filePath: url,
						name: 'file',
						formData: {},
						success: (uploadFileRes) => {
							var res = JSON.parse(uploadFileRes.data);
							console.log('uploadFilePromise',res)
							resolve(res)
						},
						fail: e => {
							resolve({code:0})
						},
					});
				})
			},

			//详情
			init() {
				if(!this.obj.id) return false
				
				this.$Http({
					url: 'doctorMyArticle',
					load: false,
					data: {id:this.obj.id},
				}).then(res => {
					this.loading = false
					if (res.code == 1 && res.data) {
						this.obj.title = res.data.title
						this.obj.keyword = res.data.keyword
						this.obj.img = res.data.img
						this.obj.content = res.data.content
						
						if (this.obj.img) this.img = [{
							status: 'success',
							message: '',
							url: this.$base.cdn(this.obj.img),
						}]
						
						this.status = res.data.auditstatus
						if(this.status==0){
							this.status_txt = 'warning'
							this.reason = '审核中'
						}else if(this.status==1){
							this.status_txt = 'success'
							this.reason = '已通过'
						}else if(this.status==2){
							this.status_txt = 'danger'
							this.reason = '未通过，原因：'+res.data.reason
						}
					}
				});
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 100,
				});
			},
			//提交
			submit() {
				this.$Http({
					url: 'doctorArticleSave',
					data: this.obj,
					show: true,
					type: 'POST'
				}).then(res => {
					console.log('submit',res)
					if (res.code == 1) {
						this.$u.toast(res.msg)
						setTimeout(function(){
							uni.navigateBack({})
						},1000)
					}
				});
			}
		}
	}
</script>

<style lang="less">
	@import '@/pagesD/style.less';
	
	
</style>
